package com.ccg.controller;

import com.ccg.entity.Message;
import com.ccg.entity.Order;
import com.ccg.entity.Order_Room;
import com.ccg.entity.User;
import com.ccg.service.OrderService;
import com.ccg.service.Order_RoomService;
import com.ccg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/admin/order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    Order_RoomService order_roomService;
    @Autowired
    UserService userService;

    //根据状态查找订单
    @RequestMapping("/getOrderByStatus")
    @ResponseBody
    public Message getOrderByStatus(int orderStatus){
        List<Order> orders = orderService.findOrderByStatus(orderStatus);
        if(orders.size()==0)
        return new Message(1,"查找订单成功","没有该状态的订单");
        else return new Message(0,"查找订单成功",orders);
    }
    //管理员对订单进行审核是否通过
    //需要order_id以及orderStatus两个参数,用Order进行封装
    //通过的orderStatus为1,不通过为-2
    @RequestMapping("/checkUserOrder")
    @ResponseBody
    public Message checkUserOrder(@RequestBody Order order){
        return orderService.checkUserOrder(order);
    }
    //用户到达前台办理入住处理逻辑
    //安排房间以及相关的信息录入
    //参数：订单主体信息、以及与之关联的订单房间信息
    @RequestMapping("/userInHotel")
    @ResponseBody
    public Message userInHotel(@RequestBody Order order){
        return orderService.userInHotel(order);
    }
    //用户到达前台办理退房逻辑
    //参数:order_id
    @RequestMapping("/orderComplete")
    @ResponseBody
    public Message orderComplete(@RequestBody Order order) throws ParseException {
        return orderService.orderComplete(order);
    }
    //用户订单取消删除一个订单房间
    @RequestMapping("/deleteOrder_Room")
    @ResponseBody
    public Message deleteOrder_Room(int or_room_id){
        Order_Room order_room = new Order_Room();
        order_room.setOr_room_id(or_room_id);
        return order_roomService.deleteOrder_Room(order_room);
    }

    //用户订单退掉一个房
    @RequestMapping("/cancelOrderRoom")
    @ResponseBody
    public Message cancelOrderRoom(@RequestBody Order_Room order_room) throws ParseException {
        if(order_roomService.outRoom2(order_room)==1)
            return new Message(0,"退房成功",order_room.getRoom_id()+"房退房成功");
        else return new Message(1,"退房失败","未知错误");
    }
    //管理员查询用户的所有订单u_id
    @RequestMapping("/adminGetUserOrders")
    @ResponseBody
    public Message adminGetUserOrders(String username){
        User user = userService.findUserByUsername(username);
        if(user==null) return new Message(1,"查找订单失败","该用户不存在");
        int u_id = user.getU_id();
        List<Order> orders = orderService.adminGetUserOrders(u_id);
        if(orders.size()>=1)
            return new Message(0,"查找用户订单成功",orders);
        else return new Message(1,"查找订单失败","该用户没有订单或没有该用户");
    }

    //根据订单order_id获取该订单的所有订单房间
    @RequestMapping("/findAllOrderRoom")
    @ResponseBody
    public Message findAllOrderRoom(int order_id){
       List<Order_Room> lists = order_roomService.findAllOrderRoom(order_id);
            return new Message(0,"查找该订单的房间成功",lists);
    }

    //订单统计功能

    //分页获取两个日期之间的订单数量
    @RequestMapping("/beforeDaysOrder")
    @ResponseBody
    public Message beforeDaysOrder(int currentPage,String startDate,String endDate){
        return new Message(0,"查找成功",orderService.beforeDaysOrder(currentPage,startDate,endDate));
    }
    //分页获取两个日期之间的订单数量
    @RequestMapping("/getOrdersInDate")
    @ResponseBody
    public Message getOrdersInDate(String startDate,String endDate){
        HashMap<String, Object> map = new HashMap<>();//哈希集合存储不重复的数据
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        return new Message(0,"查找成功",orderService.getOrdersInDate(map));
    }
    //获取两个日期之间的销售金额
    @RequestMapping("/getBeforeDaysAllMoney")
    @ResponseBody
    public Message getAllMoney(String startDate,String endDate){
        HashMap<String, Object> map = new HashMap<>();//哈希集合存储不重复的数据
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        return new Message(0,"查找成功",orderService.getAllMoney(map));
    }
}
